// myself/login/Login.js
import api from '../../utils/api.js';
import utils from '../../utils/utils.js';
import wechat from '../../utils/wechat.js';
import network from '../../utils/network.js';
import notification from '../../utils/notification_center.js'
let app = getApp();
let userInfo = null;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    finish: true,
    showTime: '获取验证码',
    second: 60,
    tempPhone: '',
    code: '',
    value1: '',
    value2: '',
    value3: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.toast = this.selectComponent("#toast");
  },

  formCode(e) {
    if (!utils.isTelNum(e.detail.value.phone)) {
      this.toast.showToast('请输入收货人正确的手机号');
      return;
    }
    if (this.data.second < 60) {
      return;
    }

    this.data.tempPhone = e.detail.value.phone;
    let that = this;
    let time = setInterval(function() {
      that.data.second = that.data.second - 1;
      that.setData({
        showTime: that.data.second == 0 ? "后获取" : that.data.second + "s后获取"
      });
      if (that.data.second == 0) {
        clearInterval(time);
        that.data.second = 60;
        that.setData({
          showTime: "重新获取"
        });
      }
    }, 1000);

    // network.postJson(this, api.sendSmsCode, {
    //   phone: e.detail.value.phone
    // }).then(res => {
    //   this.data.code = res.data.data.code
    // })
  },

  formSubmit(e) {
    // if (!e.detail.value.code) {
    //   this.toast.showToast('请输入验证码');
    //   return;
    // }
    // if (!e.detail.value.code) {
    //   this.toast.showToast('请输入验证码');
    //   return;
    // }
    // if (e.detail.value.code != this.data.code) {
    //   this.toast.showToast('验证码不正确');
    //   return;
    // }
    // if (e.detail.value.phone != this.data.tempPhone) {
    //   this.toast.showToast('手机号与验证码不匹配');
    //   return;
    // }

    // if (e.detail.value.phone != 'root' && !utils.isTelNum(e.detail.value.phone)) {
    //   this.toast.showToast('请输入收货人正确的手机号');
    //   return;
    // }

    network.postJson(this, api.login, {
      user: e.detail.value.phone,
      pass: e.detail.value.code
    }).then(res => {   
      app.globalData.userInfo = res.data.data
      wx.setStorageSync('jinxin_user_info', res.data.data)
      notification.postNotificationName('Myself')
      wx.navigateBack();
      // wx.reLaunch({
      //   url: '/pages/home/Home'
      // })
    })
  }

})